package com.hadluo.qq.server.controller;

import com.hadluo.qq.server.entity.User;
import com.hadluo.qq.server.mapper.UserMapper;
import com.hadluo.qq.server.service.FavoritesService;
import com.hadluo.qq.server.vo.AddFriendRequest;
import com.hadluo.qq.server.vo.FavoriteVO;
import com.hadluo.qq.server.vo.FriendVO;
import com.hadluo.qq.server.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/favorites")
public class FavoritesController {

    @Autowired
    private FavoritesService favoritesService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 添加好友
     * @param request 添加好友请求
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result<Void> addFavorite(@RequestBody AddFriendRequest request) {
        if (request.getQqNumber() == null || request.getFriendQQNumber() == null) {
            return Result.error("QQ号不能为空");
        }

        // 不能添加自己为好友
        if (request.getQqNumber().equals(request.getFriendQQNumber())) {
            return Result.error("不能添加自己为好友");
        }

        User user1 = userMapper.selectByQQNumber(request.getQqNumber());
        User user2 = userMapper.selectByQQNumber(request.getFriendQQNumber());
        boolean success = favoritesService.addFavorite(user1.getId(), user2.getId());
        if (success) {
            return Result.success("添加常用联系人成功");
        } else {
            return Result.error("已经是你的常用联系人啦~");
        }
    }

    /**
     * 获取好友列表
     * @param qqNumber 用户QQ号
     * @return 好友列表
     */
    @GetMapping("/list")
    public Result<List<FriendVO>> getFavoriteList(@RequestParam String qqNumber) {
        if (qqNumber == null || qqNumber.isEmpty()) {
            return Result.error("QQ号不能为空");
        }

        User user = userMapper.selectByQQNumber(qqNumber);
       System.out.println(user.toString());
        List<FriendVO> favoriteList = favoritesService.getFavoriteList(user.getId());
        return Result.success("",favoriteList);
    }

    /**
     * 获取好友详细信息
     * @param userQQNumber 用户QQ号
     * @param friendQQNumber 好友QQ号
     * @return 好友详细信息
     */
    @GetMapping("/detail")
    public Result<FriendVO> getFavoriteDetail(@RequestParam String userQQNumber, @RequestParam String friendQQNumber) {
        if (userQQNumber == null || userQQNumber.trim().isEmpty() ||
                friendQQNumber == null || friendQQNumber.trim().isEmpty()) {
            return Result.error("QQ号不能为空");
        }

        User user1 = userMapper.selectByQQNumber(userQQNumber);
        User user2 = userMapper.selectByQQNumber(friendQQNumber);
        FavoriteVO favoriteDetail = favoritesService.getFavoriteDetail(user1.getId(), user2.getId());
        if (favoriteDetail == null) {
            return Result.error("获取好友信息失败，可能不是好友关系");
        }
        FriendVO friendVO = new FriendVO();
        BeanUtils.copyProperties(favoriteDetail, friendVO);

        return Result.success("", friendVO);
    }

    /**
     * 删除好友
     * @param userQQNumber 用户QQ号
     * @param friendQQNumber 好友QQ号
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public Result<Void> deleteFriend(@RequestParam String userQQNumber, @RequestParam String friendQQNumber) {
        if (userQQNumber == null || userQQNumber.trim().isEmpty() ||
                friendQQNumber == null || friendQQNumber.trim().isEmpty()) {
            return Result.error("QQ号不能为空");
        }
        User user1 = userMapper.selectByQQNumber(userQQNumber);
        User user2 = userMapper.selectByQQNumber(friendQQNumber);
        boolean success = favoritesService.deleteFavorite(user1.getId(), user2.getId());
        if (success) {
            return Result.success("删除好友成功");
        } else {
            return Result.error("删除好友失败，可能不是好友关系");
        }
    }

}
